//ready
// 创建自定义的命名空间
(function(){

    // 定义一个查询器
    var $ = function(query){
        return new customNL(query);
    };
    // 定义NodeList查询器
    var customNL = function(query){
        query = document.querySelectorAll(query);

        this.length = query.length;
        for (var i = 0; i < this.length; i++) {
            this[i] = query[i];
        }

        return this;
    };
	
	customNL.prototype = {
			each: function(callback){
				for (var i = 0; i<this.length; i++) {
					callback.call(this[i]);
				}
		
				return this;
			},
			bind: function (type, fn, capture) {
			  return this.each(function () {
					this.addEventListener(type, fn, capture ? true : false);
			  });
			  return this;
			},
			unbind: function (type, fn, capture) {
			  return this.each(function () {
				this.removeEventListener(type, fn, capture ? true : false);
			  });
			  return this;
			}
	};
	
    var readyFn = [];
	
    var DOMReady = function(){
        for (var i = 0, l = readyFn.length; i < l; i++) {
            readyFn[i]();
        }

        // 释放内存
        readyFn = null;
        document.removeEventListener('DOMContentLoaded', DOMReady, false);
    };

    $.ready = function(fn){
        if (readyFn.length == 0) {
            document.addEventListener('DOMContentLoaded', DOMReady, false);
        }

        readyFn.push(fn);
    };

    // 声明$为全局变量
    window.WMCC = $;

})(); //立刻执行命名空间的创建


/*javascript 常用函数*/
String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function(){
	return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function(){
	return this.replace(/(\s*$)/g, "");
}
Number.prototype.format = function( i ){
	return Math.round( this * Math.pow( 10 , i )) / Math.pow( 10 , i);	
}
String.prototype.todate = function(){
	var date = Date.parse(this);
	if( isNaN( date ) ){ date = Date.parse( this.replace(/-/g,'/') ); }		
	if( isNaN( date )){ date = 0; }
	return date;
}

var $id = function( id ){
	return document.getElementById(id);
}

//-------------------------------------------

function strToDate(str){
	var arrayx=str.split("-");
    var datex=new Date(arrayx[0],arrayx[1]-1,arrayx[2]);
    return datex;
}

function dateToStr(date){

	var date = new Date(date);
	return date.getFullYear() + '-' + (date.getMonth()<10?'0'+date.getMonth()+1:date.getMonth()+1) + '-' + (date.getDate()<10?'0'+date.getDate():date.getDate())	;

}

function today(){
	var date = new Date();
	return date.getFullYear() + '-' + (date.getMonth()<10?'0'+date.getMonth()+1:date.getMonth()+1) + '-' + (date.getDate()<10?'0'+date.getDate():date.getDate())	;
}

/*  功能： 把一个日期加上n天（每一个月按30天，一年360天计算）
	入口参数：   
	thedate: 要计算的日期对象
	days: 增加的天数 
如果计算出的日期不合法： 年大于9999等，则返回false，否则true */

function addday(thedate,days){
    day=thedate.getDate();
    month=thedate.getMonth()+1;
    year=thedate.getFullYear();
    mod_y=days % 360;
    y=Math.floor(days/360);
    year+=y;                
    if (mod_y!=0){
        mod_m=mod_y % 30;
        m=Math.floor(mod_y / 30);
        month+=m;
        if (mod_m!=0){
            day+=mod_m;                     
            if (day>30){
              month++;
              day-=30;
             }
        }               
     }
    thedate.setTime(new Date(year,month-1,day)); 
    return (year<=9999);
}

/***功能： 得到两个日期相差的天数（每一个月按30天，一年360天计算）
入口参数：	
date1: 日期对象1
date2: 日期对象2
返回 date1 - date2相差的天数 ***/
function getDiffDay(date1,date2)
{
	var year=date1.getFullYear() - date2.getFullYear();
	var month=date1.getMonth() - date2.getMonth();
	var day=date1.getDate() - date2.getDate();				
	return year * 12 * 30 + month * 30 + day;
}

//判断是否为正数不包括零
function isPlus( num ){
	return /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(num);	
}

//判断非负数，包括零
function isNotMinus ( num ){
	return /^\d+(\.\d+)?$/.test(num);	
}

//正整数
function isInt( num ){
	return /^[0-9]*[1-9][0-9]*$/.test(num);
}

//非负正数包括零
function isNotMInt(){
	return /^[1-9]d*|0$/.test(num);	
}

//检查文本框内容
function checkFormValue( obj , msg){	
	if( obj.value.trim() == ''){
		
		myAlert(obj ,msg +'不能为空！');
		return false;
		
	}
	return true;
}

//
function checkFormPlus(obj , msg){
	if( !checkFormValue(obj , msg)){
		return false;	
	}else{
		if(!isPlus(obj.value) ){
			
			myAlert(obj ,msg +'必须为正整数！');
			return false;

		}
		return true;
	}
	return true;
}


function checkFormInt(obj , msg){
	if( !checkFormValue(obj , msg)){
		return false;	
	}else{
		if(!isInt(obj.value) ){

			myAlert(obj ,msg +'必须为正整数！');
			return false;
			
		}
		return true;
	}
	return true;
}


function checkFormDate(obj , msg){
	if( !checkFormValue(obj , msg)){
		return false;	
	}else{
		if(! /^\d{4}-\d{2}-\d{2}$/.test(obj.value) ){
			
			myAlert(obj ,msg +'日期格式不正确！');
			return false;
			
		}
		return true;
	}
	return true;
}


function touchmovehandler(e) { e.preventDefault(); }

function objfocus(obj){
	obj.focus();
	//obj.select();	
}



function myAlert(obj, msg, container){
	
	//if(!container){container = 'container';}

	
	disablefrm(true,"container");
	document.addEventListener('touchmove', touchmovehandler , false);
	
	art.dialog({
		content: msg,
		lock: true,
		opacity: 0.2,
		title: '温馨提示！',
		ok: function () {

			disablefrm(false,"container");
			document.removeEventListener('touchmove', touchmovehandler , false);
			
			setTimeout(objfocus(obj),800);

		},
	});	
	
	//重新绑定UI
	buildUI(WMCC);
}


function disablefrm( disabled , content){
	
	if(disabled == null){disabled = true;}
	
	var frms = ['input','select','textarea'];
	for(var i=0; i<frms.length; i++){
		var context = content?document.getElementById(content):document;
		var frm = context.getElementsByTagName(frms[i]);
		for(var j = 0; j<frm.length; j++){
				if(frm[j].getAttribute("role") != 'disabled'){
					frm[j].disabled = 	disabled;
				}
		}	
	}	
}


function buildUI($){
		$('input[type=text],select,button').bind('touchstart', function(e){
			this.className = this.className + " tap";
		}).bind('touchend',function(){
			this.className = this.className.replace(' tap', '');	
		});

		$('.submit').bind('touchstart', function(e){
			this.className = this.className + " submit_tap";
		}).bind('touchend',function(){
			this.className = this.className.replace(' submit_tap', '');	
		});
		
		$('.reset').bind('touchstart', function(e){
			this.className = this.className + " reset_tap";
		}).bind('touchend',function(){
			this.className = this.className.replace(' reset_tap', '');	
		});	
}

//日期控件
function dateBuild(o){
	
	
	SelectDate(o,'yyyy-MM-dd',0,0,10,1);	
	
	//重新绑定UI
	buildUI(WMCC);
}

//初始化UI
(function($){

	$.ready(function(){
		
		buildUI($);
		
	});
	
})(WMCC)